package com.ruipeng.shengyapo.controller;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.ruipeng.shengyapo.pay.alipay.config.AlipayConfig;

import net.sf.json.JSONObject;

public class TestController {

	public static void main(String[] args) throws Exception {
		// 推送订单消息
//		String tsStr = YunBa.yunbaUtil();
//		tsStr = tsStr.replace("#topic", "Topic_User");
//		tsStr = tsStr.replace("#alert", "您有一条订单消息");
//		tsStr = tsStr.replace("#type", "1");
//		HttpUtils.sendPost(YunBa.url, tsStr.toString());

//		Map<String,String> map = new HashMap<String,String>();
//		map.put("total_amount","0.01");
//		double amount = Double.parseDouble(map.get("total_amount"));
//		System.out.println(amount);
		//String xml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx7b14fffaff032ed6]]></appid><mch_id><![CDATA[1480763132]]></mch_id><nonce_str><![CDATA[heTvvAKOZsGcucTW]]></nonce_str><sign><![CDATA[6BA921A8B5DA5F946728D407CED652E7]]></sign><result_code><![CDATA[SUCCESS]]></result_code><prepay_id><![CDATA[wx201706121249243c3f454be80205251955]]></prepay_id><trade_type><![CDATA[APP]]></trade_type></xml>";

		//xml = xml.substring(xml.indexOf("<prepay_id><![CDATA[")+20,xml.indexOf("]]></prepay_id>"));
		//System.out.println(xml);
		//Map map = XmlUtil.xml2map(xml,false);
		//String prepay_id = (String)map.get("prepay_id");
		//System.out.println("prepay_id="+map.toString());

//		System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
//		System.out.println(TestController.class.getClassLoader().getResource(""));
//		System.out.println(ClassLoader.getSystemResource(""));
//		System.out.println(TestController.class.getResource(""));
//		System.out.println(TestController.class.getResource("/"));//Class文件所在路径
//		File file = new File("src/main/webapp/WEB-INF/apiclient_cert.p12");
//		FileInputStream instream = new FileInputStream(file);
//		System.out.println(instream);
//		String str = "appid=wx7b14fffaff032ed6&mch_id=1480763132&nonce_str=1448451569&total_fee=2&refund_fee=2&transaction_id=4003642001201706145674432370key=shengyabo1234567890shengyabo1234";
//		String sign = MD5Util.MD5Encode(str, "UTF-8")
//				.toUpperCase();
//		System.out.println(sign);
		//System.out.println();


		//支付宝退款
		AlipayClient alipayClient = new DefaultAlipayClient
				("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID,
						AlipayConfig.private_key,"json",AlipayConfig.input_charset,AlipayConfig.ali_public_key,AlipayConfig.sign_type);
		AlipayTradeRefundRequest aliRequest = new AlipayTradeRefundRequest();
		Map<String,Object> depositMap = new HashMap<String,Object>();
		String orderNo = "";
		aliRequest.setBizContent("{" +
				"\"trade_no\":\"2017061821001004520211568688\"," +
				"\"refund_amount\":0.02" +
				"  }");
		try{System.out.println("getBizContent="+aliRequest.getBizContent());
			AlipayTradeRefundResponse aliResponse = alipayClient.execute(aliRequest);
			if(aliResponse.isSuccess()){

				System.out.println("调用成功");
			} else {
				System.out.println("调用失败");
			}
		System.out.println(JSONObject.fromObject(aliResponse));
	}catch (Exception e){
			e.printStackTrace();
		}
//
//	}
//		aliRequest.setBizContent("{" +
//				"\"out_trade_no\":\"2017061580576z59GNn77b2Rjk4Au51Zu405Q5RR\"," +
//				"\"trade_no\":\"2017061521001004520206052568\"," +
//				"\"refund_amount\":0.02," +
//				"  }");

		//String orderNo = ObjUtils.getNowDate() + ObjUtils.randomNumeric(4)+"00100"+ObjUtils.randomNumeric(6)+"00"+ObjUtils.randomNumeric(4);
		//System.out.println(orderNo.length()+"+----+"+orderNo);



	}
}
